package com.x.sys.controller;

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.x.common.bo.JsonResult;
import com.x.common.bo.PageObject;
import com.x.sys.entity.Admin;
import com.x.sys.service.AdminService;

@RestController
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService ;
	
	
	
	@RequestMapping("/doLogin")
	public JsonResult doLogin(String username,String password,
   		 boolean isRememberMe) {
		 UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    	
    	 Subject currentUser = SecurityUtils.getSubject();
    	 currentUser.login(token);//将用户信息提交给securitymanager进行认证
    	 return new JsonResult("login ok");
	}
	@RequestMapping("doValidById")
	public JsonResult doValidById(Integer id,Integer valid) {
		return new JsonResult("update ok");
		}
	
	@RequestMapping("findPageObjects")
	@ResponseBody
	public JsonResult doFindPageObjects(String username,Integer pageCurrent){
		PageObject<Admin> pageObject=
		adminService.findPageObjects(username, pageCurrent);
		return new JsonResult(pageObject);
	}
	
	  @RequestMapping("doDeleteObjects")
	  @ResponseBody
	  public JsonResult dodeleteObjects(Integer... ids){
		  adminService.deleteObjects(ids);
		  return new JsonResult("delete ok");
	  }
	  
	  /**新增*/
	  @RequestMapping("doSaveObject")
	  @ResponseBody
	  public JsonResult doSaveObject(
	      		 Admin entity){
	      	    adminService.saveObject(entity);
	  return new JsonResult("save ok");    
	  }
	  
	  /**编辑*/
	  @RequestMapping("doFindObjectById")
	  public JsonResult FindObjectById(Integer id){
	     	return new JsonResult(adminService.findObjectById(id));
	  }
	  /**更新*/
	  @RequestMapping("doupdateObject")
	  public JsonResult doUpdateObject (Admin entity){
		  adminService.updateObject(entity);
	     	return new JsonResult("update ok");
	     	
	  }
	/**
	 * @exceptionHandler 注解描述的方法为控制层的异常处理方法，此注解中传入的异常类型，为它描述的方法可以处理的异常
	 * @param e
	 * @return 
	 * */
	@ExceptionHandler(value=RuntimeException.class)
	@ResponseBody
	public JsonResult doHandleRuntimeException(RuntimeException e) {
		e.printStackTrace();
		
		return  new JsonResult(e);
	}
}
